﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Web;
using MarsExplorerWebSite.Model;

namespace MarsExplorerWebSite.Functions
{
    public class getGameStatus : FunctionBase
    {
        private MarsExplorerEntities dbContext;
        string M = "Normal", S = "Normal", L = "Normal";
        public getGameStatus(HttpContext context) : base(context)
        {
            InitializeProperties();
        }

        public override string Execute()
        {
            StringBuilder sb = new StringBuilder();

            Game game = (from G in dbContext.Games select G).FirstOrDefault();
            List<Team> Teams = (from t in dbContext.Teams select t).ToList();
            List<TeamsResource> TeamsResources = (from tr in dbContext.TeamsResources select tr).ToList();
            List<TeamsTurn> TeamsTurns = (from tt in dbContext.TeamsTurns select tt).ToList();

            List<GamesEvent> ge = (from j in dbContext.GamesEvents where j.GameTurn==game.RunningTurn select j).ToList();
            foreach (GamesEvent e in ge)
            {
                switch(e.Route)
                {
                	case "M":
                        M = e.Event.Name;
                        break;
                    case "S":
                        S = e.Event.Name;
                        break;
                    case "L":
                       L = e.Event.Name;
                        break;
                }
            }
            sb.Append("------------ STATUS JOGO ------------\r\n");
            sb.Append("TURN = " + game.RunningTurn + " Lunar = " + M + " SOLAR = " + S + " LONGA = " + L + "\r\n");
            sb.Append("------------EQUIPAS ------------\r\n");
            for (int i = 0; i <= 4;i++)
            {
                sb.Append("Nome = " + Teams[i].Name + " Posicao= " + Teams[i].Route.Name + " Creditos= " + Teams[i].Credits + " Moveu = " + Teams[i].canMove+ " Mineracao = " +Teams[i].Mining + "\r\n");
            }

            sb.Append("------------RECURSOS ------------\r\n");
            sb.Append("             T1     T2     T3     T4      T5\r\n");
            sb.Append("AGUA       : " + TeamsResources[0].Value.ToString("D2") + "     " + TeamsResources[8].Value.ToString("D2") + "     " + TeamsResources[16].Value.ToString("D2") + "     " + TeamsResources[24].Value.ToString("D2") + "     " + TeamsResources[32].Value.ToString("D2") + "     \r\n");
            sb.Append("Hidrogenio : " + TeamsResources[1].Value.ToString("D2") + "     " + TeamsResources[9].Value.ToString("D2") + "     " + TeamsResources[17].Value.ToString("D2") + "     " + TeamsResources[25].Value.ToString("D2") + "     " + TeamsResources[33].Value.ToString("D2") + "     \r\n");
            sb.Append("Comida     : " + TeamsResources[2].Value.ToString("D2") + "     " + TeamsResources[10].Value.ToString("D2") + "     " + TeamsResources[18].Value.ToString("D2") + "     " + TeamsResources[26].Value.ToString("D2") + "     " + TeamsResources[34].Value.ToString("D2") + "     \r\n");
            sb.Append("Oxigenio   : " + TeamsResources[3].Value.ToString("D2") + "     " + TeamsResources[11].Value.ToString("D2") + "     " + TeamsResources[19].Value.ToString("D2") + "     " + TeamsResources[27].Value.ToString("D2") + "     " + TeamsResources[35].Value.ToString("D2") + "     \r\n");
            sb.Append("KitMedico  : " + TeamsResources[4].Value.ToString("D2") + "     " + TeamsResources[12].Value.ToString("D2") + "     " + TeamsResources[20].Value.ToString("D2") + "     " + TeamsResources[28].Value.ToString("D2") + "     " + TeamsResources[36].Value.ToString("D2") + "     \r\n");
            sb.Append("Previsao   : " + TeamsResources[5].Value.ToString("D2") + "     " + TeamsResources[13].Value.ToString("D2") + "     " + TeamsResources[21].Value.ToString("D2") + "     " + TeamsResources[29].Value.ToString("D2") + "     " + TeamsResources[37].Value.ToString("D2") + "     \r\n");
            sb.Append("Escudo     : " + TeamsResources[6].Value.ToString("D2") + "     " + TeamsResources[14].Value.ToString("D2") + "     " + TeamsResources[22].Value.ToString("D2") + "     " + TeamsResources[30].Value.ToString("D2") + "     " + TeamsResources[38].Value.ToString("D2") + "     \r\n");
            sb.Append("Fato       : " + TeamsResources[7].Value.ToString("D2") + "     " + TeamsResources[15].Value.ToString("D2") + "     " + TeamsResources[23].Value.ToString("D2") + "     " + TeamsResources[31].Value.ToString("D2") + "     " + TeamsResources[39].Value.ToString("D2") + "     \r\n");


            sb.Append("------------RECURSOS ALOCADOS ------------\r\n");
            sb.Append("             T1     T2     T3     T4      T5\r\n");
            sb.Append("AGUA       : " + TeamsTurns[0].Value.ToString("D2") + "     " + TeamsTurns[8].Value.ToString("D2") + "     " + TeamsTurns[16].Value.ToString("D2") + "     " + TeamsTurns[24].Value.ToString("D2") + "     " + TeamsTurns[32].Value.ToString("D2") + "     \r\n");
            sb.Append("Hidrogenio : " + TeamsTurns[1].Value.ToString("D2") + "     " + TeamsTurns[9].Value.ToString("D2") + "     " + TeamsTurns[17].Value.ToString("D2") + "     " + TeamsTurns[25].Value.ToString("D2") + "     " + TeamsTurns[33].Value.ToString("D2") + "     \r\n");
            sb.Append("Comida     : " + TeamsTurns[2].Value.ToString("D2") + "     " + TeamsTurns[10].Value.ToString("D2") + "     " + TeamsTurns[18].Value.ToString("D2") + "     " + TeamsTurns[26].Value.ToString("D2") + "     " + TeamsTurns[34].Value.ToString("D2") + "     \r\n");
            sb.Append("Oxigenio   : " + TeamsTurns[3].Value.ToString("D2") + "     " + TeamsTurns[11].Value.ToString("D2") + "     " + TeamsTurns[19].Value.ToString("D2") + "     " + TeamsTurns[27].Value.ToString("D2") + "     " + TeamsTurns[35].Value.ToString("D2") + "     \r\n");
            sb.Append("KitMedico  : " + TeamsTurns[4].Value.ToString("D2") + "     " + TeamsTurns[12].Value.ToString("D2") + "     " + TeamsTurns[20].Value.ToString("D2") + "     " + TeamsTurns[28].Value.ToString("D2") + "     " + TeamsTurns[36].Value.ToString("D2") + "     \r\n");
            sb.Append("Previsao   : " + TeamsTurns[5].Value.ToString("D2") + "     " + TeamsTurns[13].Value.ToString("D2") + "     " + TeamsTurns[21].Value.ToString("D2") + "     " + TeamsTurns[29].Value.ToString("D2") + "     " + TeamsTurns[37].Value.ToString("D2") + "     \r\n");
            sb.Append("Escudo     : " + TeamsTurns[6].Value.ToString("D2") + "     " + TeamsTurns[14].Value.ToString("D2") + "     " + TeamsTurns[22].Value.ToString("D2") + "     " + TeamsTurns[30].Value.ToString("D2") + "     " + TeamsTurns[38].Value.ToString("D2") + "     \r\n");
            sb.Append("Fato       : " + TeamsTurns[7].Value.ToString("D2") + "     " + TeamsTurns[15].Value.ToString("D2") + "     " + TeamsTurns[23].Value.ToString("D2") + "     " + TeamsTurns[31].Value.ToString("D2") + "     " + TeamsTurns[39].Value.ToString("D2") + "     \r\n");


            return sb.ToString();
        }

        public override void InitializeProperties()
        {
            try
            {
                dbContext = new MarsExplorerEntities();
            }
            catch (Exception e)
            {
                string message = "";
                #if DEBUG
                message += " : " + e.ToString();
                #endif
            }
        }
    }
}